# -*- encoding: utf-8 -*-

# Copyright 2010 István Szentandrási
# <etude-music-player@googlegroups.com>

# This file is part of Etude.
#
# Etude is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as 
# published by the Free Software Foundation, either version 3 
# of the License, or (at your option) any later version.
#
# Etude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with Etude. If not, see <http://www.gnu.org/licenses/>.

import gobject

from common import *
from mediainfo import MediaInfo

class MediaLib(gobject.GObject):

    """
    wrapper class for medialib manipulation
    """

    def __init__(self, client):
        self.__gobject_init__()
        self.client = client

    @check_fast(False)
    @check_wait(False)
    def get_info(self, id, cb, **kwargs):
        """
        get info about a the medialib id
        """
        return self.client.xmms.medialib_get_info(id, (lambda res: self.__get_info(res, cb, **kwargs)))
   
    def __get_info(self, res, cb, **kwargs):
        """
        Call the callback to the current_info. If there was an error, return
        empty propdict
        """
        if res.is_error():
            cb(MediaInfo(None))
        nfo = MediaInfo(res.value())
        cb(nfo, **kwargs)
        return False

    @check_cb_None()
    @check_fast(False)
    @check_wait(False)
    def rehash(self, id = 0, cb = None):
        """
        rehash the media library
        """
        return self.client.xmms.medialib_rehash(id, cb)
 
    @check_cb_None()
    @check_fast(False)
    @check_wait(False)
    def add_entry(self, file, cb = None):
        """
        add an url to the medialib
        """
        return self.client.xmms.medialib_add_entry(file, cb)

    @check_cb_None()
    @check_fast(False)
    @check_wait(False)
    def add_entry_encoded(self, file, cb = None):
        """
        add a medialib encoded url to the medialib
        """
        return self.client.xmms.medialib_add_entry_encoded(file, cb)

    @check_cb_None()
    @check_fast(False)
    @check_wait(False)
    def path_import(self, path, cb = None):
        """
        import recursively a directory
        """
        return self.client.xmms.medialib_path_import(path, cb)

    @check_cb_None()
    @check_fast(False)
    @check_wait(False)
    def path_import_encoded(self, encoded_path, cb = None):
        """
        import recursively a medialib encoded directory
        """
        return self.client.xmms.medialib_path_import_encoded(encoded_path, cb)

    @check_cb_None()
    @check_fast(False)
    @check_wait(False)
    def remove_entry(self, id, cb = None):
        """
        remove an id from the medialib
        """
        return self.client.xmms.medialib_remove_entry(int(id), cb)

    @check_cb_None()
    @check_fast(False)
    @check_wait(False)
    def property_set(self, id, key, value, source = None, cb = None):
        """
        set a medialib property
        """
        return self.client.xmms.medialib_property_set(id, key, value, source, cb)

